<template>
<view class="update_view">
    <view class="title el-after">
        当前入库药品
    </view>
    <view class="one">
        <u-form :model="form" ref="uForm" label-width="fit-content">
            <u-form-item label="药品名称" prop="dname" label-width="150">
                <u-input v-model="form.dname" />
            </u-form-item>
            <u-form-item label="药品类型" prop="dtype" label-width="150">
                <SelectYplxInputCp
                    v-model="form.dtype">   
                </SelectYplxInputCp>
            </u-form-item>
            <u-form-item label="库存" prop="dstock">
                <u-input v-model="form.dstock" />
            </u-form-item>
            <u-form-item label="剂型" prop="djiXing">
                <SelectJxInputCp
                    v-model="form.djiXing">   
                </SelectJxInputCp>
            </u-form-item>
            <u-form-item label="费用名称" prop="dcostName" label-width="150">
                <SelectFynameInputCp
                    v-model="form.dcostName">   
                </SelectFynameInputCp>
            </u-form-item>
            <u-form-item label="库存上限" prop="dstockMax" label-width="150">
                <u-input v-model="form.dstockMax" />
            </u-form-item>
            <u-form-item label="库存下限" prop="dstockMin" label-width="150">
                <u-input v-model="form.dstockMin" />
            </u-form-item>
            <u-form-item label="生产日期" prop="dproductTime" label-width="150">
                <SelectDateInputCp
                    v-model="form.dproductTime">   
                </SelectDateInputCp>
            </u-form-item>
            <u-form-item label="有效日期" prop="deffectTime" label-width="150">
                <SelectDateInputCp
                    v-model="form.deffectTime">   
                </SelectDateInputCp>
            </u-form-item>
            <u-form-item label="规格" prop="dspec">
                <SelectGgInputCp
                    v-model="form.dspec">   
                </SelectGgInputCp>
            </u-form-item>
            <u-form-item label="进价" prop="dinsertPrice">
                <u-input v-model="form.dinsertPrice" />
            </u-form-item>
            <u-form-item label="售价" prop="dsellPrice">
                <u-input v-model="form.dsellPrice" />
            </u-form-item>
            <u-form-item label="病功效" prop="efficay" label-width="150">
                <u-input v-model="form.efficay" type="textarea"/>
            </u-form-item>
        </u-form>
        <view class="bt_list">
            <u-button
                v-if="!dname"
                :loading="loading"
                type="success"
                @click="addData">
                药品入库
            </u-button>
            <u-button
                v-else
                type="error"
                @click="updatedata">
                药品修改
            </u-button>
            <u-button 
                type="error"
                @click="removeForm">
                重置
            </u-button>
        </view>
    </view>
    <u-toast ref="uToast" />
</view>
</template>

<script>
//药品入库
import SelectYplxInputCp from "@/components/public/SelectYplxInput";
import SelectJxInputCp from "@/components/public/SelectJxInput";
import SelectFynameInputCp from "@/components/public/SelectFynameInput";
import SelectGgInputCp from "@/components/public/SelectGgInput";
import SelectDateInputCp from "@/components/public/SelectDateInput";
import {addDrug,findDrugs,updateDrug} from"@/common/api";
export default {
    components:{
        SelectYplxInputCp,
        SelectJxInputCp,
        SelectFynameInputCp,
        SelectGgInputCp,
        SelectDateInputCp,
    },
    data() {
        return {
            loading:false,  //控制药品入库的加载状态
            dname:undefined,  //由外部传入
            form: {
                dcostName: "",
                deffectTime: "",
                dinsertPrice: "",
                djiXing: "",
                dname: "",
                dproductTime: "",
                dsellPrice: "",
                dspec: "",
                dstock: "",
                dstockMax: "",
                dstockMin: "",
                dtype: "",
                efficay: "",
            },
            rules:{  //表单校验
                dcostName: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                deffectTime: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dinsertPrice: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                djiXing: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dname: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dproductTime: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dsellPrice: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dspec: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dstock: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dstockMax: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dstockMin: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                dtype: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
                efficay: [
                    {
                        required: true,
                        message: '不能为空',
                        trigger: ['blur', 'change']
                    }
                ],
            },
        }
    },
    onLoad(option){
        this.dname = option.dname;
        if(option.dname){  //如果有数据表示是修改
            this.findData(option.dname);
        }
    },
    onReady() {
        //写入表单校验规则
        this.$refs.uForm.setRules(this.rules);
    },
    methods: {
        findData(value){  //查找药品
            this.loading = true;
            findDrugs(
                {
                    dname:value,
                },
            ).then(data=>{
                //赋值到表单内
                let item = data[0];
                for(let index in this.form){
                    this.form[index] = item[index];
                }
            }).finally(()=>{
                this.loading = false;
            });
        },
        addData(){  //药品入库
        	this.$refs.uForm.validate(valid => {
        		if (valid) {
                    this.loading = true;
        			addDrug(
                        this.form,
                    ).then(data=>{
                        this.$refs.uToast.show({
                            title: "成功",
                            position:'top',
                            type: 'success',
                        });
                        //成功后返回上一个页面
                        setTimeout(()=>{
                            uni.navigateBack();
                        },1000);
                    }).finally(()=>{
                        this.loading = false;
                    });
        		}
        	});
        },
        updatedata(){  //药品修改
        	this.$refs.uForm.validate(valid => {
        		if (valid) {
                    this.loading = true;
        			updateDrug(
                        this.form,
                    ).then(data=>{
                        this.$refs.uToast.show({
                            title: "成功",
                            position:'top',
                            type: 'success',
                        });
                        //成功后返回上一个页面
                        setTimeout(()=>{
                            uni.navigateBack();
                        },1000);
                    }).finally(()=>{
                        this.loading = false;
                    });
        		}
        	});
        },
        removeForm(){  //重置表单
            for(let index in this.form){
                this.form[index] = "";
            }
            this.$refs.uForm.resetFields();
        },
    },
}
</script>
<style scoped lang="scss">
.update_view{
    padding: 35rpx;
    box-sizing: border-box;
    .title{
        width: fit-content;
        font-size: 45rpx;
        color: #000000;
        font-weight: bold;
        margin-bottom: 35rpx;
    }
    >.one{
        border: 1px solid #dfeaea;
        border-radius: 5rpx;
        padding: 25rpx;
        padding-top: 5rpx;
        box-sizing: border-box;
        display: grid;
        grid-gap: 25rpx;
        font-size: 25rpx;
        margin-bottom: 0;
        .find{
            width: 100%;
            display: grid;
            grid-template-columns: 1fr min-content;
            grid-gap: 15rpx;
            /deep/button{
                padding: 0 35rpx;
            }
        }
        .bt_list{
            width: 100%;
            display: flex;
            justify-content: space-between;
            margin-top: 25rpx;
        }
    }
    >.two{
        margin-bottom: 35rpx;
    }
}
</style>
